﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace UrlChecker.Analysis
{
    public partial class ucURL : UserControl
    {
        #region Attributes

        private Thread myThread = null;

        #endregion

        #region ucURL

        public ucURL()
        {
            InitializeComponent();
        }

        #endregion

        #region Form

        private void ucURL_Load(object sender, EventArgs e)
        {
            InitComboBoxGroup();
            InitComboBoxURL();
        }

        private void ucURL_SizeChanged(object sender, EventArgs e)
        {
            this.pbClose.Location = new Point(this.Width - this.pbClose.Width - 3, 3);
        }

        #endregion

        #region Button

        private void btnCheck_Click(object sender, EventArgs e)
        {
            DataTable dt = QT.SEO.SnapShot.Get(this.cbbURL.SelectedValue.ToString());
            this.dgvResult.AutoGenerateColumns = false;
            this.dgvResult.Columns[1].DataPropertyName = "snapDate";
            this.dgvResult.Columns[2].DataPropertyName = "checkDate";
            this.dgvResult.Columns[3].DataPropertyName = "SearchName";
            this.dgvResult.Columns[4].DataPropertyName = "id";
            this.dgvResult.DataSource = dt;
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            Browser myBrowser = new Browser(this.cbbURL.GetItemText(this.cbbURL.SelectedItem), FormWindowState.Normal);
            myBrowser.Show();
        }

        #endregion

        #region PictureBox

        private void pbClose_Click(object sender, EventArgs e)
        {
            if (myThread != null && myThread.IsAlive)
            {
                if (MessageBox.Show("正在进行网址快照检查，确认要关闭吗？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                {
                    return;
                }

                myThread.Abort();
            }

            this.Dispose();
        }

        private void pbClose_MouseHover(object sender, EventArgs e)
        {
            this.pbClose.Image = Properties.Resources.close02;
        }

        private void pbClose_MouseLeave(object sender, EventArgs e)
        {
            this.pbClose.Image = Properties.Resources.close01;
        }

        #endregion

        #region DataGridView

        private void dgvResult_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            this.dgvResult.Rows[e.RowIndex].Cells[0].Value = e.RowIndex + 1;
        }

        private void dgvResult_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            ToolStripLabel tssl = ((Main)this.ParentForm).tsslMsg;
            tssl.Text = string.Format("共{0}条记录（{1}）", this.dgvResult.RowCount, this.cbbURL.GetItemText(this.cbbURL.SelectedItem));
        }

        #endregion

        #region ToolStripMenuItem

        private void ToolStripMenuItem_Export_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.ValidateNames = true;
            saveFileDialog1.Filter = "日志文件(*.log)|*.log|CSV文件(*.csv)|*.csv";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.DefaultExt = "log";
            saveFileDialog1.FileName = "";
            saveFileDialog1.AddExtension = true;
            if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }

            if (saveFileDialog1.FileName.EndsWith(".csv"))
            {
                if (QT.SEO.Export.Export2CSV(dgvResult, saveFileDialog1.FileName))
                {
                    MessageBox.Show("导出成功");
                }
                else
                {
                    MessageBox.Show("操作失败");
                    return;
                }
            }
            else
            {
                if (QT.SEO.Export.Export2Txt(dgvResult, saveFileDialog1.FileName))
                {
                    MessageBox.Show("导出成功");
                }
                else
                {
                    MessageBox.Show("操作失败");
                    return;
                }
            }
        }

        #endregion

        #region ComboBox

        private void cbbGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitComboBoxURL();
        }

        #endregion

        #region Method

        private void InitComboBoxGroup()
        {
            DataTable dt = QT.SEO.Group.GetAll();
            DataRow dr = null;

            dr = dt.NewRow();
            dr["guid"] = "0";
            dr["GName"] = "--全部--";
            dt.Rows.InsertAt(dr, 0);

            this.cbbGroup.ValueMember = dt.Columns[0].ToString();
            this.cbbGroup.DisplayMember = dt.Columns[1].ToString();
            this.cbbGroup.DataSource = dt;

            this.cbbGroup.SelectedIndex = 1;
        }

        private void InitComboBoxURL()
        {
            DataTable dt = null;

            if (this.cbbGroup.SelectedIndex == 0) dt = QT.SEO.URL.GetAll();
            else dt = QT.SEO.URL.GetAll(this.cbbGroup.SelectedValue.ToString());

            this.cbbURL.DisplayMember = "url";
            this.cbbURL.ValueMember = "guid";
            this.cbbURL.DataSource = dt;

            if (this.cbbURL.Items.Count == 0)
            {
                this.btnCheck.Enabled = false;
                this.btnView.Enabled = false;
            }
            else
            {
                this.btnCheck.Enabled = true;
                this.btnView.Enabled = true;
            }
        }

        #endregion
    }
}
